$(document).ready(function () {
    var moes = $("#moes").kendoDropDownList({
        filter: "contains",
        placeholder: "Elige el MOE...",
        dataTextField: "text",
        dataValueField: "value",
        dataSource: {
            type: "json",
            transport: {
                read: urlMoes
            }
        },
        dataBound:function(){
            if( appBosch.init && appBosch.moeId ){
                moes.value(appBosch.moeId);
                moes.value(appBosch.moeId);
                areas.refresh();
                appBosch.moeId = '1';
            }
        }
    }).data("kendoDropDownList");

    var areas = $("#areas").kendoDropDownList({
        autoBind: false,
        cascadeFrom: "moes",
        filter: "contains",
        placeholder: "Elige el Área...",
        dataTextField: "text",
        dataValueField: "value",
        dataSource: {
            serverFiltering: true,
            type: "json",
            transport: {
                read: urlAreas
            }
        },
        dataBound:function(){
            if( appBosch.init && appBosch.moeId === '' && appBosch.areaId ){
                /*var index = 0;
                while( areas.dataItem(index) !== undefined ){
                    var areaDItem = areas.dataItem(index);
                    if( areaDItem.value === appBosch.areaId ){
                        break;
                    }
                    index++;
                }
                areas.select(index);*/
                areas.value(appBosch.areaId);                
                appBosch.areaId = '1';
            }else if( appBosch.moeId === '1' ){
                appBosch.moeId = '';
            }
        }
    }).data("kendoDropDownList");

    var lines = $("#lineas").kendoDropDownList({
        autoBind: false,
        cascadeFrom: "areas",
        filter: "contains",
        placeholder: "Elige la Línea...",
        dataTextField: "text",
        dataValueField: "value",
        dataSource: {
            serverFiltering: true,
            type: "json",
            transport: {
                read: urlLines
            }
        },
        dataBound:function(){
            if( appBosch.init && !appBosch.moeId && !appBosch.areaId && appBosch.lineId ){
                lines.value(appBosch.lineId);
                appBosch.lineId = '';
                appBosch.init   = false;
                if( appBosch.nominas !== '' ){
                    $( '#employees' ).val( appBosch.nominas );
                    $( '#btnImprimir' ).click();
                }
            }else if( appBosch.areaId === '1' ){
                appBosch.areaId = '';
            }
        }
    }).data("kendoDropDownList");
});